package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjKnowledgeType;
import com.zb.entity.ZjKnowledgeTypeVideo;
import com.zb.entity.ZjVideo;
import com.zb.extend.KnowledgeTypeVideoExtend;
import com.zb.mapper.ZjKnowledgeTypeVideoMapper;
import com.zb.service.ZjKnowledgeTypeService;
import com.zb.service.ZjKnowledgeTypeVideoService;
import com.zb.service.ZjVideoService;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.vo.KnowledgeInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 知识库分类视频表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-13
 */
@Service
public class ZjKnowledgeTypeVideoServiceImpl extends ServiceImpl<ZjKnowledgeTypeVideoMapper, ZjKnowledgeTypeVideo> implements ZjKnowledgeTypeVideoService {
    @Resource
    private ZjVideoService videoService;
    @Resource
    private ZjKnowledgeTypeService knowledgeTypeService;

    // 新增、修改、 删除
    @Override
    public void save(Long custAccId, Long videoId, List<KnowledgeTypeRelationVo> knowledgeTypeVos, List<Long> delIds) {
        if (CollectionUtil.isNotEmpty(delIds)) {
            this.removeByIds(delIds);
        }
        if (CollectionUtil.isEmpty(knowledgeTypeVos)) {
            return;
        }
        List<ZjKnowledgeTypeVideo> adds = new ArrayList<>();
        List<ZjKnowledgeTypeVideo> updates = new ArrayList<>();
        ZjKnowledgeTypeVideo typeVideo = null;
        for (KnowledgeTypeRelationVo vo : knowledgeTypeVos) {
            typeVideo = new ZjKnowledgeTypeVideo();
            BeanUtil.copyProperties(vo, typeVideo);
            if (null != typeVideo.getId()) {
                updates.add(typeVideo);
                continue;
            }
            typeVideo.setId(IdWorker.getId());
            typeVideo.setCustAccId(custAccId);
            typeVideo.setVideoId(videoId);
            adds.add(typeVideo);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据视频ID获取知识库分类信息
    @Override
    public List<ZjKnowledgeTypeVideo> getByVideoId(Long videoId) {
        QueryWrapper<ZjKnowledgeTypeVideo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id");
        wrapper.eq("video_id", videoId);
        return this.list(wrapper);
    }

    // 根据视频ID删除
    @Override
    public void deleteBatchByVideoId(List<Long> videoIds) {
        if (CollectionUtil.isEmpty(videoIds)) {
            return;
        }
        QueryWrapper<ZjKnowledgeTypeVideo> wrapper = new QueryWrapper<>();
        wrapper.in("video_id", videoIds);
        this.remove(wrapper);
    }

    // 根据知识库分类ID获取视频信息
    @Override
    public List<KnowledgeTypeVideoExtend> getByKnowledgeTypeIds(List<Long> knowledgeTypeIds) {
        if (CollectionUtil.isEmpty(knowledgeTypeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeVideo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "video_id");
        wrapper.in("knowledge_type_id", knowledgeTypeIds);
        List<ZjKnowledgeTypeVideo> typeVideoList = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeVideoList)) {
            return new ArrayList<>();
        }

        List<Long> videoIds = typeVideoList.stream().map(ZjKnowledgeTypeVideo::getVideoId).distinct().toList();
        QueryWrapper<ZjVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.select("id", "name", "image");
        videoQueryWrapper.in("id", videoIds);
        List<ZjVideo> videoList = videoService.list(videoQueryWrapper);
        return typeVideoList.stream().map(type -> {
            KnowledgeTypeVideoExtend extend = new KnowledgeTypeVideoExtend();
            BeanUtil.copyProperties(type, extend);
            ZjVideo video = videoList.stream().filter(f -> Objects.equals(f.getId(), type.getVideoId())).findFirst().orElse(null);
            if (null != video) {
                extend.setName(video.getName());
                extend.setImage(video.getImage());
            }
            return extend;
        }).toList();
    }

    // 根据知识仓库分类ID获取产品视频
    @Override
    public List<KnowledgeInfoVo> getVideo(Long knowledgeTypeId) {
        List<Long> knowledgeTypeIds = new ArrayList<>();
        knowledgeTypeIds.add(knowledgeTypeId);
        List<KnowledgeTypeVideoExtend> videoExtendList = getByKnowledgeTypeIds(knowledgeTypeIds);

        QueryWrapper<ZjKnowledgeType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("name");
        typeQueryWrapper.eq("id", knowledgeTypeId);
        ZjKnowledgeType knowledgeType = knowledgeTypeService.getOne(typeQueryWrapper);

        return videoExtendList.stream().map(e -> {
            return new KnowledgeInfoVo(e.getVideoId(), e.getName(), e.getImage(), knowledgeType.getName());
        }).toList();
    }

    // 根据视频ID获取知识库信息
    @Override
    public List<KnowledgeTypeVideoExtend> getByVideoIds(List<Long> videoIds) {
        if (CollectionUtil.isEmpty(videoIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeVideo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "video_id");
        wrapper.in("video_id", videoIds);
        List<ZjKnowledgeTypeVideo> typeVideoList = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeVideoList)) {
            return new ArrayList<>();
        }
        List<Long> knowledgeTypeIds = typeVideoList.stream().map(ZjKnowledgeTypeVideo::getKnowledgeTypeId).distinct().toList();
        QueryWrapper<ZjKnowledgeType> knowledgeTypeQueryWrapper = new QueryWrapper<>();
        knowledgeTypeQueryWrapper.select("id", "name");
        knowledgeTypeQueryWrapper.in("id", knowledgeTypeIds);
        List<ZjKnowledgeType> knowledgeTypeList = knowledgeTypeService.list(knowledgeTypeQueryWrapper);

        return typeVideoList.stream().map(type -> {
            KnowledgeTypeVideoExtend extend = new KnowledgeTypeVideoExtend();
            BeanUtil.copyProperties(type, extend);
            extend.setName(knowledgeTypeList.stream().filter(f -> Objects.equals(f.getId(), type.getKnowledgeTypeId())).map(ZjKnowledgeType::getName).findFirst().orElse(""));
            return extend;
        }).toList();
    }
}
